/* eslint-disable eol-last */
/* eslint-disable indent */
'use strict';

const { deleteFollowerFailInfo, addFollowerFailInfo } = require('../utils/ErrorModel');
const ErrorModel = require('../utils/ErrorModel');
const { SuccessModel } = require('../utils/resultModel');

module.exports = app => {
    class ProfileController extends app.Controller {
        async index() {
                // 在session中获取用户信息
                const { userName } = this.ctx.session.getUserInfo;
                // 重定向
                await this.ctx.redirect(`/profile/${userName}`);

            }
            // 重定向个人主页
        async profileUser() {
                // 获取当前的用户名
                const { userName: curUserName } = this.ctx.params;
                const { userName, id: userId } = this.ctx.session.getUserInfo;
                let isMe = true;
                let curUserInfo = {};
                console.log(curUserName, userName);
                if (curUserName !== userName) {
                    // 如果不相等说明查看不是本人信息
                    isMe = false;
                    // 获取当前用户信息，先判断这个用户是否存在
                    const result = await this.service.user.getUserInfo(curUserName);

                    if (result) {
                        curUserInfo = result;
                    } else {
                        await this.ctx.render('404.html');
                        return;
                    }
                } else {
                    curUserInfo = this.ctx.session.getUserInfo;
                }
                // 获取博客信息 service
                const result = await this.service.blog.getProfileBlogList({ userName: curUserName, pageSize: 5 });

                // 获取粉丝列表
                const { userList: fansList, count: fansCount } = await this.service.userRelation.getUsersByFollowerId(curUserInfo.id);
                // 获取关注列表
                const { userList: followList, count: followCount } = await this.service.userRelation.getFollower(curUserInfo.id);
                // 判断我是不是这个人的粉丝
                const isMeFollow = fansList.some(fans => {
                    return fans.userName === userName;
                });

                // 获取 atme 数量
                const atCount = await this.service.atRelation.getAtCount(userId);

                // 最终返回的数据包含博客信息，用户基本信息
                await this.ctx.render('profile.html', {
                    userData: {
                        atCount,
                        userInfo: curUserInfo,
                        fansData: { // 粉丝
                            count: fansCount,
                            fansList,
                        },
                        followData: { // 关注
                            count: followCount,
                            followList,
                        },
                        isMe,
                        isMeFollow,
                    },
                    blogList: result.blogList,
                    pageIndex: 1, //  当前页码
                    pageSize: 5, // 每页显示的数量
                    count: result.count, // 微博的总数量
                });
            }
            // 关注
        async follow() {
                // 获取id session中获取本人id
                const { id: myUserId } = this.ctx.session.getUserInfo;
                // 获取followerId 前端传递过来的被关注人id
                const { followerId } = this.ctx.request.body;
                // 调用 service
                try {
                    const result = await this.service.userRelation.addFollower(myUserId, followerId);
                    this.ctx.body = new SuccessModel(result);
                } catch (err) {
                    // 添加关注失败
                    this.ctx.body = new ErrorModel(addFollowerFailInfo);
                }
            }
            // 取消关注


        async unFollow() {
            const { id: myUserId } = this.ctx.session.getUserInfo;
            const { followerId } = this.ctx.request.body;
            const result = await this.service.userRelation.deleteFollower(myUserId, followerId);
            console.log(result);
            if (result) {
                this.ctx.body = new SuccessModel();
                return;
            }
            this.ctx.body = new ErrorModel(deleteFollowerFailInfo);
        }
    }
    return ProfileController;
};